package cn.yudao.ymcc.redis.listener;

import cn.yudao.ymcc.service.IKillActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

//消息过期监听器
@Slf4j
@Component
public class RedisExpireListener extends KeyExpirationEventMessageListener {

    @Autowired
    private IKillActivityService killActivityService;

    public RedisExpireListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    //当消息过期，触发方法
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        // 发布=============================
        if (expiredKey.contains("activityKey:")){
            String[] split = expiredKey.split("activityKey:");
            Long activityId = Long.valueOf(split[1]);
            killActivityService.publish(activityId);
        }
    }


}

